package com.jjyu.controller;

import com.jjyu.entity.Feedback;
import com.jjyu.entity.Result;
import com.jjyu.entity.User;
import com.jjyu.service.FeedbackService;
import com.jjyu.service.UserService;
import com.jjyu.utils.JwtUtil;
import com.jjyu.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    private FeedbackService feedbackService;

    @PostMapping("/recommendations")
    public ResponseEntity<Feedback> submitRecommendationFeedback(@RequestBody Feedback feedback) {
        Feedback submittedFeedback = feedbackService.submitFeedback(feedback);
        return new ResponseEntity<>(submittedFeedback, HttpStatus.CREATED);
    }


    @PostMapping("/login")
    @Transactional
    public Result login(String name, String password) {
        User user = userService.findByUserName(name);
        if (user == null) {
            return Result.error("用户不存在");
        } else {
            if (user.getPassword().equals(Md5Utils.stringToMD5(password)) || user.getPassword().equals(password)) {
                Map<String, Object> claims = Map.of("userId", user.getUserId(), "name", user.getName(), "email", user.getEmail());
                return Result.success(Map.of("token", JwtUtil.generateToken(claims)));
            } else {
                return Result.error("密码错误");
            }
        }

    }

    @PostMapping("/register")
    public Result register(User user) {
        //查询用户
        String name = user.getName();
        String email = user.getEmail();
        String password = user.getPassword();
        User nameUser = userService.findByUserName(name);
        User mailUser = userService.findByEmail(email);
        if (nameUser == null && mailUser == null) {
            // 注册代码
            User register = userService.register(name, email, password);
            if (register != null) {
                return Result.success(register);
            }
        } else {
            if (mailUser != null) {
                return Result.error("邮箱已存在");
            } else {
                return Result.error("用户名已存在");
            }
        }
        return Result.error("注册失败");
    }


    @GetMapping("/findByName/{name}")
    public Result findByName(@PathVariable String name) {
        User user = userService.findByUserName(name);
        if (user == null) {
            return Result.error("用户不存在");
        }
        return Result.success(user);
    }


    @PatchMapping("/update")
    public Result update(String name, String email, String password) {
        User user = userService.findByUserName(name);
        if (user == null) {
            return Result.error("用户不存在");
        }
        user.setEmail(email);
        user.setPassword(password);
        boolean b = userService.updateById(user);
        if (!b) {
            return Result.error("更新失败");
        }
        User reUser = userService.findByUserName(user.getName());
        return Result.success(reUser);
    }

    @DeleteMapping("/delete")
    public Result delete(String name) {
        // 删除用户，删除用户之前先看一下用户是否在其他表中存在
        //Map<String, Object> nameMap = Map.of("name", name);
        //boolean remove = userService.removeByMap(nameMap);
        User user = userService.findByUserName(name);
        if (user == null) {
            return Result.error("用户不存在");
        } else {
            boolean remove = userService.deleteUser(user);
            if (remove) {
                return Result.success("删除成功");
            }
            return Result.error("删除失败");
        }

    }

    @GetMapping("/list")
    public Result<List<User>> list() {
        List<User> userList = userService.list();
        // 打印所有的用户
        userList.stream().toList().forEach(System.out::println);
        return Result.success(userList);
    }


}
